package com.example.librarysystem.entity;

import com.example.librarysystem.constants.Constants;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

@Data
@ApiModel(value="接口返回对象")
public class R implements Serializable {
    @ApiModelProperty(value = "返回代码")
    private Integer code;
    @ApiModelProperty(value = "返回处理消息")
    private String msg;
    @ApiModelProperty(value = "返回数据")
    private Object data;
    @ApiModelProperty(value = "数据大小")
    private Long count;

    public R(){}

    public R(int code,String msg,Object data){
        this.code=code;
        this.msg=msg;
        this.data=data;
        if(data!=null){
            this.count=(long)data.toString().length();
        }
    }
    public static R ok(){return new R(Constants.OK_CODE,Constants.OK_MSG,null);}
    public static R ok(Object data){return new R(Constants.OK_CODE,Constants.OK_MSG,data);}
    public static R ok(String msg,Object data){return new R(Constants.OK_CODE,msg,data);}
    public static R fail(String msg){return new R(Constants.FAIL_CODE,msg,null);}
    public static R fail(int errorCode,String msg){return new R(errorCode,msg,null);}

}
